from datetime import datetime
from typing import Optional, List

from pydantic import EmailStr, BaseModel, ConfigDict, Field

from app.schemas.system_schemas import RoleResponse


class UserInDB(BaseModel):
	"""数据库层内部使用的模型（含敏感字段）"""
	id: int
	user_code: str
	username: str
	name: str
	email: EmailStr
	phone: Optional[str] = None
	hashed_password: str
	disabled: bool = False
	created_at: Optional[datetime] = None
	updated_at: Optional[datetime] = None
	last_login: Optional[datetime] = None

	model_config = ConfigDict(from_attributes=True)


class UserResponse(BaseModel):
	"""对外响应模型（不含敏感字段）"""
	id: int
	user_code: str
	username: str
	name: str
	email: EmailStr
	phone: Optional[str] = None
	disabled: bool = False
	created_at: Optional[datetime] = None
	updated_at: Optional[datetime] = None
	last_login: Optional[datetime] = None

	model_config = ConfigDict(from_attributes=True)


class UserCreate(BaseModel):
	"""用户注册请求体"""
	username: str = Field(..., min_length=3, max_length=50, pattern=r"^[a-zA-Z0-9_-]+$")
	name: str = Field(..., min_length=1, max_length=100)
	email: EmailStr
	phone: Optional[str] = None
	password: str = Field(..., min_length=6, max_length=128)


class UserProfileResponse(BaseModel):
	"""用户个人信息响应模型（包含角色信息）"""
	id: int
	user_code: str
	username: str
	name: str
	email: EmailStr
	phone: Optional[str] = None
	disabled: bool = False
	created_at: Optional[datetime] = None
	updated_at: Optional[datetime] = None
	last_login: Optional[datetime] = None
	roles: List[RoleResponse] = []

	model_config = ConfigDict(from_attributes=True)


# 解决前向引用问题
UserProfileResponse.model_rebuild()
